const { statusBarHeight } = wx.getWindowInfo();
const app = getApp();
var ctx;
//导入验证js
import WxValidate from "../../../utils/WxValidate";
Page({
  data: {
    notLoggedIn: false,//是否未登录
    showLogOff: false,//退出登录弹窗
    tabbarIndex: 1,//1:登录 2:注册
    statusBarHeight: statusBarHeight,
    text: '',//验证码数据
    registerForm: {
      type: '1',//注册类别1:个人 2法人
      account: '',//账号
      password: '',//密码
      confirmPassword: '',//确认密码
      unitName: '',//单位名称
      creditCode: '',//统一社会信用代码
      userName: '',//联系人姓名
      userPhone: '',//联系人电话
    },

    registerDataMsg: {
      accountMsg: '',
      oldPasswordMsg: '',
      passwordMsg: '',
      confirmPasswordMsg: '',
      unitNameMsg: '',//单位名称
      creditCodeMsg: '',//统一社会信用代码
      userNameMsg: '',//联系人姓名
      userPhoneMsg: '',//联系人电话
    }
  },

  onLoad() {
    this.initValidate(this.data.tabbarIndex);
  },
  onShow() {

  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },//验证函数
  initValidate(index) {
    // 验证字段的规则
    const rules = {
      account: {
        required: true,
        maxlength: 15,
      },
      oldPassword: {
        required: true,
      },
      password: {
        required: true,
        password: true,
        minlength: 8,
        maxlength: 15,
      },
      confirmPassword: {
        required: true,
        password: true,
        minlength: 8,
        maxlength: 15,
        equalTo: 'password',
      },
      unitName: {
        required: true,
        maxlength: 30,
      },
      userName: {
        required: true,
        maxlength: 10,
      },
      userPhone: {
        required: true,
      },

    }
    const messages = {
      account: {
        required: '请输入账号',
        maxlength: '账号长度不多于15位',
      },
      oldPassword: {
        required: '请输入旧密码',
      },
      password: {
        required: '请输入新密码',
        minlength: '密码长度不少于8位',
        maxlength: '密码长度不多于15位',
        password: '密码中必须包含字母、数字，至少8个字符'
      },
      confirmPassword: {
        required: '请输入确认密码',
        minlength: '密码长度不少于6位',
        maxlength: '密码长度不多于15位',
        password: '密码中必须包含字母、数字，至少8个字符',
        equalTo: '确认密码和新密码保持一致',
      },
      unitName: {
        required: '请输入单位名称',
        maxlength: '单位名称长度不多于30位',
      },
      // creditCode: {
      //   required: '请输入统一社会信用代码',
      // },
      userName: {
        required: '请输入联系人姓名',
        maxlength: '联系人姓名长度不多于10位',
      },
      // assistance: {
      //   required: '请勾选1-2个敲码助手',
      // },
      userPhone: {
        required: '请输入联系人电话',
        phone: '请输入正确的电话号码格式',
      },
      // verificationCode: {
      //   required: '请输入验证码',
      // }
    }
    this.WxValidate = new WxValidate(rules, messages)
    // 自定义验证规则
    this.WxValidate.addMethod('verificationCode', (value, param) => {
      this.setData({
        'registerDataMsg.verificationCodeMsg': ''
      })
      return this.WxValidate.optional(value) || (value.toLowerCase() == this.data.text.toLowerCase())
    }, '验证码不正确')
  },
  radioChange(e) {
    const value = e.detail.value
    const radio = this.data.radio
    const items = radio.map(n => {
      return Object.assign({}, n, {
        checked: n.value === value,
      })
    })
    this.setData({
      radio: items,
      'form.gender': value,
    })
  },
  checkboxChange(e) {
    const values = e.detail.value
    const checkbox = this.data.checkbox
    const items = checkbox.map(n => {
      return Object.assign({}, n, {
        checked: values.includes(n.value),
      })
    })
    this.setData({
      checkbox: items,
      'form.assistance': values,
    })
  },
  //调用验证函数(登录)
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    // 重置校验
    this.setData({
      'dataMsg.accountMsg': '',
      'dataMsg.passwordMsg': ''
    })
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList
      console.log(error)
      error.filter((item) => {
        let itemName = 'dataMsg.' + item.param + 'Msg'
        this.setData({
          [itemName]: item.msg
        })
      })

      return false
    }
    // 显示登录后的界面
    // this.setData({ 'notLoggedIn': false });
    this.showModal({
      msg: '提交成功'
    })
  },


  //调用验证函数(注册)
  formRegister: function (e) {
    console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    // 重置校验
    this.setData({
      'registerDataMsg.accountMsg': '',
      'registerDataMsg.passwordMsg': '',
      'registerDataMsg.confirmPasswordMsg': '',
      'registerDataMsg.unitNameMsg': '',
      'registerDataMsg.creditCodeMsg': '',
      'registerDataMsg.userNameMsg': '',
      'registerDataMsg.userPhoneMsg': '',
      'registerDataMsg.verificationCodeMsg': '',
    })
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList
      console.log(error)
      // this.showModal(error)
      error.filter((item) => {
        let itemName = 'registerDataMsg.' + item.param + 'Msg'
        this.setData({
          [itemName]: item.msg
        })
      })
      var that = this;
      this.drawPic(that);
      return false
    }
    this.showModal({
      msg: '提交成功'
    })
  },
  // 监听数值变化(注册)
  changeRegisterValue(e) {
    let name = e.currentTarget.dataset.name
    let itemName = 'registerForm.' + name
    this.setData({//把输入框的值存储(绑定)到变量inputValue中
      [itemName]: e.detail.value
    });
    // 重置校验
    let itemNameMsg = 'registerDataMsg.' + name + 'Msg'
    this.setData({
      [itemNameMsg]: '',
    })
    const params = this.data.registerForm
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList
      error.filter((item) => {
        // 只动态判断正在编辑的项
        if (item.param == name) {
          let itemName = 'registerDataMsg.' + item.param + 'Msg'
          this.setData({
            [itemName]: item.msg
          })
        }
      })
    }
  },
  // 注册类别切换
  typeChange(e) {
    this.setData({
      'registerDataMsg.accountMsg': '',
      'registerDataMsg.passwordMsg': '',
      'registerDataMsg.confirmPasswordMsg': '',
      'registerDataMsg.unitNameMsg': '',
      'registerDataMsg.creditCodeMsg': '',
      'registerDataMsg.userNameMsg': '',
      'registerDataMsg.userPhoneMsg': '',
      'registerDataMsg.verificationCodeMsg': '',
    })
    this.setData({ 'registerForm.type': e.detail.value });
    this.initValidate(2)
  },

  toUserEdit() {
    wx.navigateTo({
      url: `/pages/usercenter/userEdit/userEdit`,
    });
  },
  //注销登录
  logOff() {
    console.log('切换账号')
  },
  closeDialog(e) {
    this.setData({ 'showLogOff': false });
    if (e.type == "confirm") {
      this.setData({ 'notLoggedIn': true });
    }
  },

  // 复制
  copyBtn(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.ordernum,
      success: function (res) {
        wx.showToast({
          title: '联系方式已复制',
          icon: 'none'
        });
      }
    })
  },
})
